package com.wish.g1.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.common.core.utils.poi.ExcelUtil;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.web.page.TableDataInfo;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.finance.entity.Product;
import com.wish.g1.finance.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 金融产品表 前端控制器
 * </p>
 *
 * @author wxh
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 查询金融产品列表
     */
    @RequiresPermissions("finance:product:list")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.setEntity(product);
        wrapper.eq("del_flg",0);
        List<Product> list = productService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出金融产品列表
     */
    @RequiresPermissions("finance:product:export")
    @Log(title = "金融产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.setEntity(product);
        List<Product> list = productService.list(wrapper);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "金融产品数据");
    }

    /**
     * 获取金融产品详细信息
     */
    @RequiresPermissions("finance:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.getById(id));
    }

    /**
     * 新增金融产品
     */
    @RequiresPermissions("finance:product:add")
    @Log(title = "金融产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        return toAjax(productService.save(product));
    }

    /**
     * 修改金融产品
     */
//    @RequiresPermissions("finance:product:edit")
    @Log(title = "金融产品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult edit(@RequestBody Product product)
    {
        return toAjax(productService.updateById(product));
    }

    /**
     * 删除金融产品
     */
    @RequiresPermissions("finance:product:remove")
    @Log(title = "金融产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.removeByIds(Arrays.asList(ids)));
    }
}
